package com.alibaba.dubbo.remoting.transport.netty.learn;

import java.io.IOException;

import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.Codec2;
import com.alibaba.dubbo.remoting.buffer.ChannelBuffer;

/**
 * @author 作者 E-mail: lgrui1988@163.com
 * @date 创建时间：2017年1月16日 下午1:44:58
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class MockUpCodec2 implements Codec2 {

	public void encode(Channel channel, ChannelBuffer buffer, Object message) throws IOException {
		System.out.println("ENCODER MSG:" + message.getClass().getName());
		if (message instanceof MockUpMsg) {
			MockUpMsg msg = (MockUpMsg) message;
			buffer.writeBytes(msg.getMessage());
		}
	}

	public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
		int readable = buffer.readableBytes();
		byte[] message = new byte[readable];
		buffer.readBytes(message);
		return new MockUpMsg(message);
	}

}
